package cn.seapon.talker.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public abstract class TalkerBasedFragment extends Fragment {
    protected static final String title = "消息列表";

    protected String content = "???";
    protected Context context;
    
    
    public TalkerBasedFragment(Context context ,String content) {
		super();
		this.context = context;
       StringBuilder builder = new StringBuilder();
        for (int i = 0; i < 200; i++) {
            builder.append(content).append(" ");
        }
        builder.deleteCharAt(builder.length() - 1);
        this.content = builder.toString();
	}
    
    protected abstract void initData();
    protected abstract void initView();
    
    
    protected void toastShow(String str){
    	Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }

    public String getTitile(){
    	return title;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if ((savedInstanceState != null) && savedInstanceState.containsKey(title)) {
            content = savedInstanceState.getString(title);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ScrollView scrollView = new ScrollView(context);
    	TextView text = new TextView(getActivity());
        text.setGravity(Gravity.CENTER);
        text.setText(content);
        text.setTextSize(20 * getResources().getDisplayMetrics().density);
        text.setPadding(20, 20, 20, 20);
        scrollView.addView(text);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        layout.setGravity(Gravity.CENTER);
        layout.addView(scrollView);

        return layout;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(title, content);
    }
}
